package vn.huync.smsbackupandrestore;

import java.util.ArrayList;

import vn.huync.smsbackupandrestore.models.ContactModel;
import vn.huync.smsbackupandrestore.models.SmsModel;
import vn.huync.smsbackupandrestore.others.Utils;
import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Environment;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.dropbox.client2.session.AccessTokenPair;
import com.dropbox.client2.session.AppKeyPair;
import com.dropbox.client2.session.Session.AccessType;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.services.drive.Drive;
import com.nch.smsbackupandrestore.R;

public class MyApp extends Application{
	
	final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;
	
	private static MyApp instance = null;
	
	public static MyApp GetInstance(){
		if(instance == null){
			instance = new MyApp();
		}
		return instance;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		lSms = new ArrayList<SmsModel>();
		lContact = new ArrayList<ContactModel>();
		
		String APP_KEY = getString(R.string.api_key);
		String APP_SECRET = getString(R.string.api_secret);
		AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
		AndroidAuthSession session = new AndroidAuthSession(appKeys, ACCESS_TYPE);
		mDBApi = new DropboxAPI<AndroidAuthSession>(session);
		
		//check loged in or not
		isLogedInDropbox = false;
		SharedPreferences pref = this.getSharedPreferences("smsbackup", Activity.MODE_PRIVATE);
		String keyStore = pref.getString("key", "");
		if(keyStore.equals(""))
			isLogedInDropbox = false;
		else{
			AccessTokenPair accessTokenPair = GetAccessTokenPair();
			mDBApi.getSession().setAccessTokenPair(accessTokenPair);
			isLogedInDropbox = true;
		}
		// login google drive
		Utils.autoLoginDrive(this, getApplicationContext());
		
		sdCardPath = Environment.getExternalStorageDirectory().toString();
		boolean isSaveSdCard = pref.getBoolean("is_save_sdcard", true);
		if(isSaveSdCard){
			boolean isSDPresent = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
			if(isSDPresent)				// if has sdcard
				appFolderPath = sdCardPath+"/SmsBackup/";
			else{						// have not sdcard
				Editor editor = pref.edit();
				editor.putBoolean("is_save_sdcard", false);
				editor.commit();
				appFolderPath = ""+getCacheDir();
			}
		}
		else appFolderPath = ""+getCacheDir();
	}
	
	AccessTokenPair GetAccessTokenPair()
	{
		SharedPreferences pref = this.getSharedPreferences("smsbackup", Activity.MODE_PRIVATE);
		String keyDropbox = pref.getString("key", "");
		String secretDropbox = pref.getString("secret", "");
		if(keyDropbox.equals("") != true && secretDropbox.equals("")!= true)
			return new AccessTokenPair(keyDropbox, secretDropbox);
		else return null;
	}
	
	public String sdCardPath;
	public String appFolderPath;
	public String filePath;
	public ArrayList<SmsModel> lSms;
	public ArrayList<ContactModel> lContact;
	public boolean isLogedInDropbox;
	public DropboxAPI<AndroidAuthSession> mDBApi;
	public Drive service;
	public GoogleAccountCredential credential;
}
